﻿
namespace Tong.Serialize
{

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;

    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;


    using System.Xml;
    using System.IO;
    using System.Xml.Serialization;
    using Tong.Serialize;

    //加入序列化類型
    ///二進制序列化
    using System.Runtime.Serialization.Formatters.Binary;


    /// <summary>
    /// XML序列化类型
    /// </summary>
    public class SerializeXML
    {

        /// <summary>
        /// 用于序列化的 write 
        /// </summary>
        private System.IO.StringWriter Write = new System.IO.StringWriter();

        /// <summary>
        /// 开始定义用于序列化的新实例
        /// </summary>
        private System.Xml.Serialization.XmlSerializer MySerializeXml;

        /// <summary>
        /// 用於資料讀出的資料實例
        /// </summary>
        private System.IO.StringReader SR;


        /// <summary>
        /// 使用XML序列化
        /// </summary>
        /// <param name="type">要傳入的類型的實例</param>
        /// <returns></returns>
        public string SerializeClassToXML(object type)
        {
            MySerializeXml = new System.Xml.Serialization.XmlSerializer(type.GetType()); ;
            MySerializeXml.Serialize(Write, type);
            return Write.ToString();
        }

        /// <summary>
        /// 類型反序列化
        /// </summary>
        /// <param name="XMLstring">類型序列化後的XML字符串</param>
        /// <param name="type">傳入當前類型的類型</param>
        /// <returns></returns>
        public object SerializeXMLToClass(string XMLstring, Type type)
        {
            SR = new System.IO.StringReader(XMLstring);
            MySerializeXml = new System.Xml.Serialization.XmlSerializer(type);
            return MySerializeXml.Deserialize(SR);

        }
    }

    /// <summary>
    /// 以SOAP可格式對對象 序列化的類
    /// </summary>
    public class SerializeSOAP
    {

        /// <summary>
        /// 流對象用於對資料的返回
        /// </summary>
        System.IO.MemoryStream StreamMemory;

        /// <summary>
        /// 用SOAP序列化對像的實例操作方法
        /// </summary>
        System.Runtime.Serialization.Formatters.Soap.SoapFormatter MySerializeSOAP;

        /// <summary>
        /// 傳入一個對像把它返回成 一個記憶體的流
        /// </summary>
        /// <param name="objectModel">序列化的對像</param>
        /// <returns></returns>
        public System.IO.MemoryStream SerializeClassToStream(object objectModel)
        {
            //實例化操作的各對象
            StreamMemory = new MemoryStream();
            MySerializeSOAP = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
            //開始執行操作
            MySerializeSOAP.Serialize(StreamMemory, objectModel);

            return StreamMemory;

        }


        /// <summary>
        ///  傳入一個記憶體的流把它返回成 一個對像
        /// </summary>
        /// <param name="Streamobj">返序列化的流</param>
        /// <returns></returns>
        public object SerializeStreamToClass(MemoryStream Streamobj)
        {
            //用於返回反序列華的類型的結果
            object Serializeobject;
            //實例化操作各對的對像
            MySerializeSOAP = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();

            Serializeobject = MySerializeSOAP.Deserialize(StreamMemory);

            StreamMemory.Close();//關了
            StreamMemory.Dispose();//清理

            return Serializeobject;
        }
    }

    /// <summary>
    /// 序列化类型
    /// </summary>
    public class SerializeBinary
    {
        #region 以下代碼會報 錯 類SerializeBinary 不包含 SerializeClassToStream 的定義 不知道為什麽 2009-1-28



        /// <summary>
        /// 流對象用於對資料的返回
        /// </summary>
        System.IO.MemoryStream StreamMemory;

        /// <summary>
        /// 用SOAP序列化對像的實例操作方法
        /// </summary>
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter MySerializeBinary;

        /// <summary>
        /// 傳入一個對像把它返回成 一個記憶體的流
        /// </summary>
        /// <param name="objectModel">序列化的對像</param>
        /// <returns></returns>
        public System.IO.MemoryStream SerializeClassToStream(object objectModel)
        {
            //實例化操作的各對象
            StreamMemory = new MemoryStream();
            MySerializeBinary = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            //開始執行操作
            MySerializeBinary.Serialize(StreamMemory, objectModel);

            ///获取或设置流中的当前位置
            ///將當前的位置前移到 0 
            ///http://msdn.microsoft.com/zh-cn/library/system.io.memorystream_members(VS.95).aspx
            StreamMemory.Position = 0;

            return StreamMemory;

        }


        /// <summary>
        ///  傳入一個記憶體的流把它返回成 一個對像
        /// </summary>
        /// <param name="Streamobj">返序列化的流</param>
        /// <returns></returns>
        public object SerializeStreamToClass(MemoryStream Streamobj)
        {
            //用於返回反序列華的類型的結果
            object Serializeobject;
            //實例化操作各對的對像
            MySerializeBinary = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            Serializeobject = MySerializeBinary.Deserialize(StreamMemory);

            StreamMemory.Close();//關了
            StreamMemory.Dispose();//清理

            return Serializeobject;
        }


        #endregion

    }


}